package com.dayuan.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.dayuan.bean.Order;
import com.dayuan.bean.OrderInfo;
import com.dayuan.dao.BackOrderMapper;
import com.dayuan.dao.OrderInfoMapper;
import com.dayuan.vo.BackOrderVo;

@Service
public class BackOrderService {
	private static Logger logger = LoggerFactory.getLogger(BackOrderService.class);
	@Resource
	private BackOrderMapper backOrderMapper;
	@Resource
	private OrderInfoMapper orderInfoMapper;

	/**
	 * 后台根据商户id查询订单
	 * 
	 * @param mid
	 * @return
	 * @throws Exception
	 */
	public List<Order> selectOrderByMid(Integer mid) throws Exception {
		try {
			return backOrderMapper.selectOrderByMid(mid);
		} catch (Exception e) {
			logger.error("后台查询订单失败" + e.getMessage());
			throw new Exception("网络不稳定，请稍候再试");
		}
	}

	
	/**
	 * 商家更新订单
	 * @param order
	 * @return
	 * @throws Exception
	 */
	public int updateOrder(Order order) throws Exception {
		try {
			return backOrderMapper.updateOrder(order);
		} catch (Exception e) {
			logger.error("后台更新订单失败" + e.getMessage());
			throw new Exception("网络不稳定，请稍候再试");
		}
	}
	
	/**
	 * 根据订单id，查询订单
	 * @param orderId
	 * @return
	 * @throws Exception
	 */
	public Order selectOrderByOrderId(Long orderId) throws Exception{
		try {
			return backOrderMapper.selectOrderByOrderId(orderId);
		} catch (Exception e) {
			logger.error("后台更新订单失败" + e.getMessage());
			throw new Exception("网络不稳定，请稍候再试");
		}
	}

	/**
	 * 后台根据订单号查询订单详情
	 * 
	 * @param orderId
	 * @return
	 * @throws Exception
	 */
	public List<OrderInfo> selectByOrderId(Long orderId) throws Exception {
		try {
			return orderInfoMapper.selectByOrderId(orderId);
		} catch (Exception e) {
			logger.error("后台查询订单详情失败" + e.getMessage());
			throw new Exception("网络不稳定，请稍候再试");
		}
	}
    
	/**
	 * 根据商家查询总行数
	 * 
	 * @return
	 */
	public int selectCountOrderByParam(BackOrderVo backOrderVo) {
		return backOrderMapper.selectCountOrderByParam(backOrderVo);
	}
     
	/**
	 *根据商家按页查询订单
	 * 
	 * @param startIndex
	 * @param pageNum
	 * @return
	 */
	public List<Order> selectCountOrderByParamForPage(BackOrderVo backOrderVo,Integer startIndex, Integer pageNum) {
		return backOrderMapper.selectCountOrderByParamForPage(backOrderVo,startIndex,pageNum);
	}
	
	/**
	 * 根据订单id查询详情总行数
	 */
	public int selectCountByOrderId(Long orderId) {
		return orderInfoMapper.selectCountByOrderId(orderId);
	}
	
	public List<OrderInfo> selectCountByOrderIdForPage(Long orderId,Integer startIndex, Integer pageNum) {
		return orderInfoMapper.selectCountByOrderIdForPage(orderId,startIndex, pageNum);
	}
	
	
	public List<Order> selectOrders(Integer mid) {
		return backOrderMapper.selectOrders(mid);
	}
}
